const Controller = require('egg').Controller;

class WxuserController extends Controller {
  // 微信用户登录
  async login() {
    let userData = this.ctx.request.body;
    let rel = await this.ctx.service.wxpage.wxLogin(userData);
    console.log(rel);
    this.ctx.body = {
      code: 20000,
      message: '用户登录成功',
      data: rel
    };
  }
  // 用户下单
  async pay() {
    try {
      let data = this.ctx.request.body;
      const rel = this.ctx.service.wxpage.createOrder(data);
      this.ctx.body = {
        code: 20000,
        message: '下单成功'
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 用户获取订单列表
  async getOrderListData() {
    try {
      let query = this.ctx.request.query;
      let rel = await this.ctx.service.wxpage.getOrderList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取订单列表成功',
        data: rel
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误',
      };
    }
  }
  // 用户订单详情获取
  async getOrderDetailData() {
    try {
      let id = this.ctx.params.id;
      let rel = await this.ctx.service.wxpage.getOrderDetail(id);
      this.ctx.body = {
        code: 20000,
        message: '获取订单信息成功',
        data: rel
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误',
      };
    }
  }
  // 确认收货
  async confirmOrderData() {
    try {
      let id = this.ctx.params.id;
      let rel = await this.ctx.service.wxpage.confirmOrder(id);
      this.ctx.body = {
        code: 20000,
        message: '确认收货成功'
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误',
      };
    }
  }
  // 再付款
  async payOrderData() {
    try {
      let id = this.ctx.params.id;
      let rel = await this.ctx.service.wxpage.payOrder(id);
      this.ctx.body = {
        code: 20000,
        message: '付款成功'
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误',
      };
    }
  }
  // 删除订单
  async deleteOrderData() {
    try {
      let id = this.ctx.params.id;
      let rel = await this.ctx.service.wxpage.deleteOrder(id);
      this.ctx.body = {
        code: 20000,
        message: '删除订单成功'
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误',
      };
    }
  }
}

module.exports = WxuserController;
